/** 页面加载类 ~ 工作台 v1.0.0 **/
var homeIndex = {
	init: function () { //初始化
		var shortcutMenuData = homeIndex.getSysShortcutmenu();//加载快捷菜单
		var ggtz = "";
		//加载系统版本
		homeIndex.getConfig(kaValue.homeIndex.SysVersion, function (re) {
			configValue = re.configValue;
		})
		//加载系统公告
		homeIndex.getConfig(kaValue.homeIndex.SysGGTZ, function (re) {
			ggtz = re.configValue;
			if (ggtz == undefined || ggtz == null && ggtz == "") {
				ggtz = `<span style="color: #ddd;font-size: large;">` + kaValue.homeIndex.msg.SysGGTZ +`</span>`
			}
		})
		var menuCommonArray = [];
		homeIndex.getMenucommon("clickCount", function (re) { 
			menuCommonArray = re.data;
			for(var i in menuCommonArray){
				menuCommonArray[i].cb = function (that) {
					var id = $(that).attr("menu-id");
					var title = $(that).attr("menu-title");
					var type = $(that).attr("menu-open-type");
					var url = $(that).attr("menu-url");
					ka.menuView(id, title, type, url);
				}
			}
			
		})
		var json11 = kaHtml.getjon({ //签到
			type: 3,
			body: { id: "signinDiv2_body" },
			clsTop: "5px"
		});
		var json12 = kaHtml.getjon({ //快捷入口
			number: 12,
			type: 2,
			title: "快捷入口",
			body: shortcutMenuData,
			clsTop: "5px",
			height: "240px",
			md: [{
				title: '快捷方式管理',
				id: "whgl001",
				cb: function () {
					homeIndex.loadingShortcutMenuTree();//加载快捷菜单树
				}
			}],
		});
		var json211 = kaHtml.getjon({ //待办事项
			type: 4,
			body: {
				"id": "todoGrid",
				"fsv": {},
				"fv": [],//查询条件
				"button": {
					type: 4, //【1:URL,2:本地JSON,3:外站URL,4:自定义JSON】
					datajson: [
						{
							id: "lookIds",
							title: "审批",
							cb: function (d) {
								alert(1);
							}
						}
					]
				},
				"column": [
					{ headerName: '发送人', field: 'senderId', kaSort: true },
					{ headerName: '内容', field: 'name', kaSort: true },
					{ headerName: '时间', field: 'createTime', kaSort: true }
				]
			},
			height: "386px",
		});
		var json22 = kaHtml.getjon({ //系统版本更新公告
			number: 4,
			title: "系统版本更新公告",
			body: `
			<table class="layui-table" style="background: #ddd0;">
				<tbody>
					<tr>
						<td style="background-color: #f5f7f7;width: 80px;text-align: center;padding: 0px 10px;">系统版本</td>
						<td><a href="javascript:void(0);" onclick="homeIndex.openViewSysVersion('` + configValue + `')">` + configValue + `</a></td>
					</tr>
					<tr>
						<td style="background-color: #f5f7f7;width: 80px;text-align: center;padding: 0px 10px;">更新时间</td>
						<td>2024年11月21日 15:56:37</td>
					</tr>
					<tr>
						<td style="text-align: center;font-weight: 900;font-size: 18px;color: #000;animation: animate 5s linear alternate infinite;border-bottom: 0px solid #eee;" colspan="2">系统公告</td>
					</tr>
					<tr>
						<td style="height: 235px;padding: 5px 10px;" colspan="2">
							<p style="height:100%;width: 100%;overflow: auto;word-break: break-all;">
							` + ggtz + `
							</p>
						</td>
					</tr>
				</tbody>
			</table>
			`,
			clsTop: "5px",
			clsLeft: "5px",
			height: "386px"
		});
		var json31 = kaHtml.getjon({ //常用菜单
			number: 6,
			type: 5,
			title: "常用菜单",
			body: menuCommonArray,
			clsTop: "5px",
			height: "405px"
		});
		var sysNewstypeData = homeIndex.getSysNewstypeList();//加载新闻类型
		var sysNewsabstractData = homeIndex.getSysNewsabstractList();//加载新闻摘要
		var json32 = kaHtml.getjon({ //新闻中心
			number: 6,
			type: 6,
			body: { //[{ id: "1", name: "公司新闻" }, { id: "2", name: "党建新闻" }, { id: "3", name: "行业新闻" }, { id: "4", name: "客户新闻" }]
				types: sysNewstypeData,
				datas: sysNewsabstractData
			},
			clsTop: "5px",
			clsLeft: "5px",
			height: "405px"
		});
		var json1 = kaHtml.getjon({ number: 3 });// 第一行
		json1["child"] = [];
		json1.child.push(json11); //签到
		json1.child.push(json12); //快捷入口
		var json21 = kaHtml.getjon({ // 第二行 第一列
			number: 8,
			clsTop: "5px",
			clsLeft: "5px",
		});
		var json2 = kaHtml.getjon({ number: 9 }); // 第一行
		json21["child"] = [];
		json21.child.push(json211); //第二行 第一列
		json2["child"] = [];
		json2.child.push(json21); //待办事项
		json2.child.push(json22); //快捷入口
		var json3 = kaHtml.getjon({}); // 第三行
		json3["child"] = [];
		json3.child.push(json31); //签到
		json3.child.push(json32); //快捷入口
		var json = [];
		json.push(json1);
		json.push(json2);
		json.push(json3);
		kaHtml.init("homeIndexDivHtml", json, 0);
	},
	getMenucommon: function (code, callback) {//获取常用菜单
		var option = kaValue.getUrl(kaValue.homeIndex.url.getMenucommon);
		option.url = option.url + "?orderByColumn=" + code + "&isAsc=descending";
		option.async = false;
		option.success = function (re) {
			if (re.code == 200) {
				if (callback != undefined && callback != null && callback != "") {
					callback(re);
				}
			}
		}
		ka.ajax(option);
	},
	getConfig: function (code, callback) {//获取配置
		var option = kaValue.getUrl(kaValue.homeIndex.url.getConfig);
		option.url = option.url + "/" + code;
		option.async = false;
		option.success = function (re) {
			if (re.code == 200) {
				if (callback != undefined && callback != null && callback != "") {
					callback(re);
				}
			}
		}
		ka.ajax(option);
	},
	getSysShortcutmenu: function () { //加载快捷入库
		var option = kaValue.getUrl(kaValue.homeIndex.url.getSysShortcutmenu);
		var shortcutMenuData = [];
		option.success = function (re) {
			if (re.code == 200) {
				var data = re.data;
				for (var i in data) {
					var smd = {
						id: data[i].id, // 禁止数字
						title: data[i].title,
						icon: data[i].icon,
						menuOpenType: data[i].menu.openType,
						menuType: data[i].menu.menuType,
						menuUrl: data[i].menu.path,
						menuId: data[i].menu.menuId,
						menuTitle: data[i].menu.menuName,
						cb: function (that) {
							var id = $(that).attr("menu-id");
							var title = $(that).attr("menu-title");
							var type = $(that).attr("menu-open-type");
							var url = $(that).attr("menu-url");
							ka.menuView(id, title, type, url);
						}
					};
					shortcutMenuData.push(smd);
				}
			} else {
				kaMsg.error(re);
			}
		}
		ka.ajax(option);
		return shortcutMenuData;
	},
	loadingShortcutMenuTree: function () { //加载快捷菜单树
		var uuid = ka.uuidStr();
		layui.layer.open({
			id: uuid,
			type: 1, // page 层类型
			area: ['40%', '80%'],
			title: '<i class="icon iconfont icon-renwutiaodu1 loadingShortcutMenuTree_i"></i>' + `
				<div class="loadingShortcutMenuTree_div">添加快捷方式</div>
				<div class="loadingShortcutMenuTree_div2">每个用户都有独立的快捷菜单</div>
			`,
			shade: [0.6, '#FFF'], // 点击遮罩区域，关闭弹层
			maxmin: false, // 允许全屏最小化
			anim: 0, // 0-6 的动画形式，-1 不开启
			content: `
				<div class="loadingShortcutMenuTree_content_div">
					<div id="kjlkPageTree"></div>
				</div>
			`,
			success: function () {
				var result =[];
				//加载菜单
				var option = kaValue.getUrl(kaValue.homeIndex.url.getRouters);
				option.async = false;
				option.success = function (re) { if (re.code == 200) { result = re.data; } }
				ka.ajax(option);
				// 树
				layui.tree.render({
					elem: '#kjlkPageTree',
					id: 'kjlkPageTree',
					data: result,
					showCheckbox: true,  // 是否显示复选框
					onlyIconControl: true,  // 是否仅允许节点左侧图标控制展开收缩
					isJump: true, // 是否允许点击节点时弹出新窗口跳转
					oncheck: function(obj){
						//点击处理用户快捷菜单
						var urls = kaValue.homeIndex.url.deleteSysShortcutmenuByUserId; //删除
						if(obj.checked){ urls = kaValue.homeIndex.url.addSysShortcutmenuByUserId; } //新增
						var option2 = kaValue.getUrl(urls);
						option2.data = { title: obj.data.title, id: obj.data.id };
						option2.async = false;
						option2.success = function (re) {
							if (re.code == 200) {
								var result1 = re.data;
								var cid = [];
								for(var i in result1){
									cid.push(result1[i].menu.menuId);
								}
								layui.tree.setChecked('kjlkPageTree', cid);
							}
						}
						ka.ajax(option2);
					}
				});
				var option1 = kaValue.getUrl(kaValue.homeIndex.url.getSysShortcutmenu);
				option1.async = false;
				option1.success = function (re) {
					if (re.code == 200) {
						var result1 = re.data;
						var cid = [];
						for(var i in result1){
							cid.push(result1[i].menu.menuId);
						}
						layui.tree.setChecked('kjlkPageTree', cid);
					}
				}
				ka.ajax(option1);
			}
		});
	},
	openViewSysVersion: function(name) { // 打开系统版本
		var resultHtml = "";
		var option1 = kaValue.getUrl(kaValue.homeIndex.url.SysSystemversionlogGetInfoByCode);
			option1.async = false;
			option1.data = {
				name: name
			}
			option1.success = function (re) {
				if (re.code == 200) {
					var result1 = re.data;
					for(var i in result1){
						if(i == 0){
							resultHtml = resultHtml + `
								<div class="layui-timeline-item">
									<i class="icon iconfont icon-yizhihang1 layui-timeline-axis"></i>
									<div class="layui-timeline-content layui-text">
									<h3 class="layui-timeline-title">` + result1[i].createTime + `<span style="font-size: 14px;color: #939393;float: right;margin-right: 55px;">` + result1[i].name + `</span></h3>
									<p>
										` + result1[i].content + `
									</p>
									</div>
								</div>
							`;
						} else {
							resultHtml = resultHtml + `
								<div class="layui-timeline-item">
									<i class="icon iconfont icon-calendar-alt layui-timeline-axis" style="color: #bbb;"></i>
									<div class="layui-timeline-content layui-text">
									<h3 class="layui-timeline-title">` + result1[i].createTime + `<span style="font-size: 14px;color: #939393;float: right;margin-right: 55px;">` + result1[i].name + `</span></h3>
									<p>
										` + result1[i].content + `
									</p>
									</div>
								</div>
							`;
						}
					}
				}
			}
			ka.ajax(option1);
		var uuid = ka.uuidStr();
		layui.layer.open({
			id: uuid,
			type: 1, // page 层类型
			area: ['35%', '80%'],
			title: ``,
			shade: [0.6, '#FFF'], // 点击遮罩区域，关闭弹层
			maxmin: false, // 允许全屏最小化
			anim: 0, // 0-6 的动画形式，-1 不开启
			content: `
			<div class="layui-timeline" style="width: 95%;margin: 0 auto;height: 98%;overflow: auto;">
				` + resultHtml + `
			</div>
			`,
			success: function () {
			}
		});
	},
	getSysNewstypeList: function () {//加载新闻类型
		var sysNewstypeData = [];
		var option = kaValue.getUrl(kaValue.homeIndex.url.SysNewstypeList);
		option.data = {
			type: 0,
			status: 1
		} 
		option.async = false;
		option.success = function (re) {
			if (re.code == 200) {
				sysNewstypeData = re.data;
			}
		}
		ka.ajax(option);
		return sysNewstypeData
	},
	getSysNewsabstractList:function () {
		var sysNewsabstractData = [];
		var option = kaValue.getUrl(kaValue.homeIndex.url.SysNewsabstractList);
		option.data = {
			status: 1
		} 
		option.async = false;
		option.success = function (re) {
			if (re.code == 200) {
				var reData = re.data;
				var numberMap = new Map();
				for(var j in reData){
					var number = numberMap.get(reData[j].typeId);
					if(number == undefined || number == null || number == ""){
						number = 1;
						numberMap.set(reData[j].typeId,number);
					} else {
						number = number + 1;
						if(number > 5){
							break;
						} else {
							numberMap.set(reData[j].typeId,number);
						}
					}
					sysNewsabstractData.push({
						id: reData[j].id,
						name: reData[j].name,
						type: reData[j].typeId,
						synopsis: reData[j].abstracts,
						createTime: reData[j].publicationTime,
						img: reData[j].bsackgroundImage
					});
				}
			}
		}
		ka.ajax(option);
		return sysNewsabstractData
	}
}